import cv2
# 顶帽=原图-开运算（腐蚀+膨胀）
img = cv2.imread('./1.jpg')
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))
graden = cv2.morphologyEx(img, cv2.MORPH_TOPHAT, kernel)
cv2.imshow('img', img)
cv2.imshow('graden', graden)
cv2.waitKey(0)